<?
    cria_variaveis($_POST,$_GET,$_FILES);
    require_once("./classes/upload.php");
?> 
<p class="titulo" align="center"><?php echo $msg->get('CHOLESKY_MSG_02') ?></p>
<p class="subtitulo" align="center"><?php echo $msg->get('CHOLESKY_MSG_03') ?></p>
<p>
<div class="tabbar">
    <ul>
    <li><a href="index.php?inc=cholesky.php&modo=material"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material") echo " class=\"tabact\""; ?>><?php echo $msg->get('Material_Teorico') ?></a></li>
    <li><a href="index.php?inc=cholesky.php&modo=exemplo"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") echo " class=\"tabact\""; ?>><?php echo $msg->get('Exemplos_Prontos') ?></a></li>
    <li><a href="index.php?inc=cholesky.php&modo=teclado"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") echo " class=\"tabact\""; ?>><?php echo $msg->get('Entrada_Teclado') ?></a></li>
    </ul>
</div>
<div id="cas">
<?php
if (!isset($Calcular)) {
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material"){ //pagina inicial
        $uploads = new Upload();
        $uploads->Lista("sistemas");
    }
    if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") { //leitura de dados via banco de dados - modulo de exemplos e exercicios
	    include "exibe_prob_sela_cholesky_xml.php"; // exibe menu de exemplos contidos na biblioteca de exemplos em xml
	}
	else if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") {
        if ((strlen($dim)==0) | ($dim<=0))  { 
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=cholesky.php&modo=teclado>";
            echo $msg->get('CHOLESKY_MSG_04');
            echo "<input type=text size='2' maxlength='2' name=dim value=".($dim).">";
            echo "<br><br>";
            echo "<input type=hidden name=executar value=sim>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_02').">";
            echo "</form>"; 
            echo "<form action=index.php?opcao=cholesky method=post>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        }
        elseif (!checaNumerico($dim))
        {
    ?>
            <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
            <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
    <?php
        }
        else {
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=cholesky.php&modo=teclado>";
            //echo CHOLESKY_MSG_04;
            echo "<input type=hidden name=dim value=".($dim).">";
            //echo "<br><br>";
            echo "<p align=left><font face=Arial>".$msg->get('CHOLESKY_MSG_05')."</font>";
            echo "<nobr>";
            for ($i=0; $i<$dim; $i++) {
                echo "<br><br>";
                for ($j=0; $j<$dim; $j++) {
                    $aux=$j+1;
                    echo "<input type=text maxlength=10 size=5 name=A[".($i)."][".($j)."] value=".($A[$i][$j])."> X <sub> ".($aux)." </sub>";
                    if ($j<($dim-1)) {
                        echo " + ";
                    } //end if
                } //end for j
                echo " = ";
                echo "<input type=text maxlength=10 size=5 name=T[".($i)."] value=".($T[$i]).">";
            } //end for i
            echo "</nobr>";
            echo "<p>";
            echo "<input class=button type=submit name=Calcular value=".$msg->get('G_MSG_04').">";
            echo "<br>";
            echo "</form>";
            echo "<form method=post enctype=multipart/form-data action=index.php?inc=cholesky.php>";
            echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
            echo "</form>";
        } //end if
    }
} else {
	// armazena os dados do problema para serem resolvidos por outros metodos
	for ($i=0; $i<$dim; $i++) {
	   for ($j=0; $j<$dim; $j++) {
		$Atemp[$i][$j]=$A[$i][$j];
	   } //end for j
		$Atemp[$i][$j]=$T[$i];
	} //end for i
    if ($modo != "teclado")
        verificaBib($xml_file);
    if (!checaNumerico($dim) || !checaNumerico($A) || !checaNumerico($T))
    {
?>
        <p><font color="red"><?php echo $msg->get('Apenas_Dados_Numericos') ?></font></p>
        <p><input type="button" value="<?php echo $msg->get('Voltar') ?>" onclick="history.back()" class="button"></p>
<?php
    }
    else
    {
        echo "<br><b>".$msg->get('CHOLESKY_MSG_04')."</b> $dim";
    
        echo "<br>";
        echo "<p align=left><font face=Arial><b>".$msg->get('CHOLESKY_MSG_05')."</b></font>";
        echo "<table border=\"0\">\n";
        for ($i=0; $i<$dim; $i++) {
            echo "<tr>\n";
            for ($j=0; $j<$dim; $j++) {
                $aux=$j+1;
                if ($j>0) {
                    echo "<td valign=\"top\" align=\"center\" width=\"20\"> + </td>";
                } //end ifj
                echo "<td align=\"right\" valign=\"top\">".$A[$i][$j]."</td>";
                echo "<td align=\"right\" valign=\"top\">x<sub>".$aux."</sub></td>";
            } //end forj
            echo "<td valign=\"top\" align=\"center\" width=\"20\"> = </td>";
            echo "<td valign=\"top\" align=\"right\">$T[$i]</td>";
            echo "\n</tr>\n";
        } //end fori
        echo "</table>";
        /******************inicializar************************/
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            $U[$i][$j]=0;
            $Ut[$i][$j]=0; 
        } //forj
        } //fori
        
        /***************************TRANSPOSICAO******************************/
        function transpose($M) {
        
        global $dim,$Ut;
        
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            $Ut[$i][$j]=$M[$j][$i];	
        } // forj
        } // fori
        
        } //transpose
        
        /************************************** retrosubsstituicao ************************************************/
                                        
        function retrosubs($B) {
        
        global $dim,$VetX;
        
        $VetX[$dim-1]=$B[$dim-1][$dim]/$B[$dim-1][$dim-1];
        for ($k=$dim-2; $k>=0; $k--) { 
            $VetX[$k]=$B[$k][$dim];
            for ($i=$k+1; $i<$dim; $i++) { 				
                $VetX[$k]-=$B[$k][$i]*$VetX[$i];
            } // fori
            $VetX[$k]/=$B[$k][$k];
        } // end fork							
        
        } // end retrosubss
        
        /**********************************substituicaoforward*****************************************/
        
        function fwsub($f) {
        
        global $dim,$VetY;
            
        $VetY[0]=$f[0][$dim]/$f[0][0];
        for ($i=1; $i<$dim; $i++) {
            $S=0;
            for ($j=0; $j<=$i-1; $j++) {
                $S+=$f[$i][$j]*$VetY[$j];
            } //forj
            $VetY[$i]=($f[$i][$dim]-$S)/$f[$i][$i];
        } //fori
        
        } //fwsub
        
        /****************** VERIFICAR SE A MATRIZ E SIMETRICA ****************************/
        
        for ($i=1; $i<$dim; $i++) {
            for ($j=0; $j<$dim; $j++) {    
                if ($A[$i][$j]!=$A[$j][$i]) {
            $sim=1;
                }  //if
            } //forj
        } //fori
    
        /* Verifica se o problema e definido positivo */
        $naodecomp = 0;
        for ($aux=1; $aux <= $dim; $aux++)
        {
            if ($aux == 1) //primeira submatriz (1x1);
                $result[0] = $A[0][0];
            else if ($aux == 2) //segunda submatriz (2x2);
                $result[1] = $A[0][0]*$A[1][1] - $A[0][1]*$A[1][0];
            else
            { // para as submatrizes maiores que 2x2
                for ($i=0; $i<$aux; $i++)
                {
                    for ($j=0; $j<$aux; $j++)
                    {
                        $B[$i][$j]=$A[$i][$j];
                    }
                } //end for i
                $d=1;
                for ($k=0; $k<$aux; $k++)
                {
                    $p=$B[$k][$k];
                    for ($i=0; $i<$aux; $i++)
                        $t[$i]=$B[$i][$k];
                    for ($j=$k; $j<$aux ;$j++)
                    {
                        @$B[$k][$j]/=$p;
                        for ($i=0; $i<$aux; $i++)
                        {
                            if ($k!=$i)
                                $B[$i][$j]-=$t[$i]*$B[$k][$j];
                        } // end for i
                    } //end for j
                    $d = $d * $p; //fator de correcao do determinante
                } //end for k
                for ($i=0; $i<$aux; $i++)
                    $d*=$B[$i][$i];
                $result[$aux-1] = $d;
            } //end else
            if ($result[$aux-1] <= 0) $naodecomp = 1;
        } //end for aux
    
        if (@$sim == 1)
        {
            echo "<br><b>".$msg->get('CHOLESKY_MSG_10')."</b><br>";
        }
        else if ($naodecomp == 1)
        {
            echo "<br><b>".$msg->get('CHOLESKY_MSG_13')."</b><br>";
    
            echo "\n<script language=\"javascript\" type=\"text/javascript\">\n";
            for ($k=1; $k<=$dim; $k++)
            {
                echo "function formulario$k() {\n";
                echo "window.open('calcdet.php', 'submatriz$k', 'toolbar=no, location=no, directories=no, scrollbars=yes,resizable=yes,menubar=no,width=400,height=320');\n";
                echo "document.matriz$k.submit();\n";
                echo "}\n";
            }
            echo "</script>";
            $positiva = 0;
            for ($k=1; $k<=($dim); $k++)
            {
                echo "<br>";
                if ($result[$k-1] == 0) { $positiva = 1; }
                if ($k < $aux) { echo "".$msg->get('DECMAT_MSG_11')." A<sub>$k</sub>: "; }
                echo "<br><br>";
                echo "<form action=calcdet.php method=post name=matriz$k target=submatriz$k><table>";
                for ($i=0; $i<$dim ; $i++)
                {
                    echo "<tr>";
                    for ($j=0; $j<$dim ; $j++)
                    {
                        if (($i<$k)and($j<$k))
                        {
                            echo "<td bgcolor=#FFCFAB weight=25 width=25 valign=center align=center cellpading=0 cellspacing=0>";
                            echo "<input type=hidden name=A[$i][$j] value=".$A[$i][$j].">";
                        }
                        else
                            echo "<td weight=25 width=25 valign=center align=center cellpading=0 cellspacing=0>";
                        echo $A[$i][$j];
                        echo "</td>";
                    } //end for
                    echo "</tr>";
                } //end for
                echo "<input type=hidden name=dim value=$k>";
                echo "</table></form>";
                echo "<br><a href=\"javascript:void(0)\" onClick=\"formulario$k()\">";
                if ($k < $aux)
                {
                    echo "<b>".$msg->get('DECMAT_MSG_12')." $k x $k = </b></a>";
                    if ($result[$k-1]>0)
                        echo "<font color=blue>".$result[$k-1]."</font>";
                    else
                        echo "<font color=red>".$result[$k-1]."</font>";
                }
                echo "<br><br><br>";
            }
    
        }
        else
        {
    
        /**************** FIM DA VERIFICACAO DE MATRIZES DEFINIDAS POSITIVAS **************/
    
        if ($sim!=1) {      //se a matriz for simetrica
        
        /***************** inicializar valores****************/
        for ($i=0; $i<$dim; $i++) { 
            for ($j=0; $j<=$dim; $j++) {
            $U[$i][$j]=0;
            $Ut[$i][$j]=0;
            } //end for j
        } //end for i
        
        /*************metodo Cholesky*********************************/
        
        global $A,$U,$Ut,$dim,$p,$T,$VetY;
        
        for ($i=0; $i<$dim; $i++) {
            for ($j=0; $j<$dim; $j++) {
            if ($i==$j) {
            $somatmp=0;
                for ($k=0; $k<$j; $k++) {
                    $somatmp+=$U[$k][$i]*$U[$k][$i];
            } //fork
            $U[$i][$i]=sqrt($A[$i][$i]-$somatmp);
            } //if
            if ($j>$i) {
            $somatmp=0;
            for ($k=0; $k<$i; $k++) {
                $somatmp+=$U[$k][$i]*$U[$k][$j];
            } //fork
            $U[$i][$j]=(1/$U[$i][$i])*($A[$i][$j]-$somatmp);
            } //if
            } //forj
        } //fori
        
        transpose($U);
        
        for ($i=0; $i<$dim; $i++) {
            for ($j=0; $j<=$dim;$j++) {
                if ($j==$dim) {
                $Ut[$i][$j]=$T[$i];
                } //if
                else {
                $Ut[$i][$j]=$Ut[$i][$j];
                } //else  
            } //forj
        } //fori
        
        fwsub($Ut);
        
        for ($i=0; $i<$dim; $i++) {
            for ($j=0; $j<=$dim;$j++) {
                if ($j==$dim) {
                $U[$i][$j]=$VetY[$i];
                } //if
                else {
                $U[$i][$j]=$U[$i][$j];
                } //else  
            } //forj
        } //fori         
        
        retrosubs($U);
        
        /********************************************************************************************************/
        echo "<p align=left><font face=Arial><b>".$msg->get('CHOLESKY_MSG_08')."</b></font>";
        //echo "<nobr>";
        echo "<table border=\"0\">";
        for ($i=0; $i<$dim; $i++) {
            //echo "<br>";
            echo "<tr>";
            for ($j=0; $j<$dim; $j++) {
                echo "<td width=\"50\">";
                //echo "<input type=text name=U[".($i)."][".($j)."] value=".($U[$i][$j])." maxlength=10 size=5>";
                echo $U[$i][$j]."</td>";
            } //end for j
            echo "</tr>";
        } //end for i
        //echo "</nobr>";
        echo "</table>";
        echo "<p align=left><font face=Arial><b>".$msg->get('CHOLESKY_MSG_09')."</b></font>";
        //echo "<nobr>";
        echo "<table border=\"0\">";
        for ($i=0; $i<$dim; $i++) {
            //echo "<br>";
            echo "<tr>";
            for ($j=0; $j<$dim; $j++) {
                echo "<td width=\"50\">".$Ut[$i][$j]."</td>";
                //echo "<input type=text name=Ut[".($i)."][".($j)."] value=".($Ut[$i][$j])." maxlength=10 size=5>";
            } //end for j
            echo "</tr>";
        } //end for i
        //echo "</nobr>";
        echo "</table>";
        echo "<p align=left><font face=Arial><b>".$msg->get('CHOLESKY_MSG_11')."</b></font>";
        echo "<br>";
        echo "x = (";
        for ($i=0; $i<$dim; $i++) {
                echo $VetX[$i];
                if ($i!=$dim-1) {echo ",";}
                echo "<input type=hidden name=VetX[".($i)."] maxlength=10 size=5 value=".($VetX[$i]).">";
        } //end fori
        echo ") <sup>T</sup>";
        echo "<p>";
        seqlsol($A,$VetX,$dim,$vet_resp);
        if ($correcao=="sim") {
            echo "<br>";
            echo "<font face=Arial><b>".$msg->get('CHOLESKY_MSG_12')."</b></font>";
            echo "<br>";
            echo "x = (";
            for ($i=0; $i<$dim; $i++) {
                echo $vet_resp[$i];
                if ($i!=$dim-1) {echo ",";}
                    echo "<input type=hidden name=vet_resp[".($i)."] maxlength=10 size=5 value=".($vet_resp[$i]).">";
            } //end fori
            echo ") <sup>T</sup>";
        } // end if
        echo "<p>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=cholesky.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
    
            } //if teste de simetria da matriz
        } //else (definida positiva)
    
        if ($naodecomp == 1 || $sim == 1)  {   //se a matriz nao for simetrica ou nao for definida positiva
        echo "<p align=left>";
            //<font face=Arial>".(CHOLESKY_MSG_10)."</font>";
        echo "<p>";
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=cholesky.php>";
        echo "<input class=button type=submit name=Submit value=".$msg->get('G_MSG_03').">";
        echo "</form>";
        }  //end else
    
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
            echo "<script language=\"javascript\" type=\"text/javascript\">\n";
            echo "function criarLinks(value) {\n";
            echo "document.exemplos.indice.value = value;\n";
            echo "document.exemplos.action = 'index.php?inc=cholesky.php&modo=exemplo'\n";
            echo "document.exemplos.submit();\n";
            echo "}\n";
            echo "</script>";
            echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=\"hidden\" name=\"indice\">";
            echo $msg->get('Outros_Exemplos')."<br>";
            $objeto = new DOMDocument();
            $objeto->load($xml_file);
            $filho = $objeto->getelementsbytagname("lin_systems");
            if ($filho->length > 0)
            {
            $temp = $filho->item(0)->getelementsbytagname("system");
                $j = 0;
                for($i = 0; $i < $temp->length; $i++)
                {
                    $method = $temp->item($i)->getelementsbytagname("method");
                    $conteudo = $method->item(0)->nodeValue;
                    $dime = $temp->item($i)->getelementsbytagname("dim");
                    $dime = $dime->item(0)->nodeValue;
                    if ($conteudo == 'direct')
                    {
                        if ($j != 0) echo " | ";
                        if ($j == $indice) echo "<b>";
                        echo "<a href=\"javascript:criarLinks($j)\" title=\"Dimens&atilde;o $dime\"><u>".($j+1)."</u></a>";
                        if ($j == $indice) echo "</b>";
                        $j++;
                    }
                }
            }
            echo "</form>";
        }
    
            echo "<p>";
        echo $msg->get('G_PROXIMO_EXEMPLO');
        echo "</p>";
    
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=ludecomp.php>";
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=hidden name=navegacao value=sim>";
            echo "<input type=hidden name=indice value=$indice>";
        }
        echo "<input type=hidden name=dim value=".($dim).">";
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
        } //end for j
        echo "<input type=hidden name=T[".($i)."] value=".($Atemp[$i][$j]).">";
        } //end for i
        echo "<input class=button type=submit name=Calcular value='".$msg->get('LUDECOMP_MSG_03')."'>";
            echo "</form>";
    
    
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss1.php>";
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=hidden name=navegacao value=sim>";
            echo "<input type=hidden name=indice value=$indice>";
        }
        echo "<input type=hidden name=dim value=".($dim).">";
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
        } //end for j
        echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
            } //end for i
        echo "<input class=button type=submit name=Calcular value='".$msg->get('GAUSS_MSG_03a')."'>";
            echo "</form>";
    
        echo "<form method=post enctype=multipart/form-data action=index.php?inc=gauss2.php>";
        if (isset($_POST["navegacao"]) && $_POST["navegacao"] == "sim")
        {
            echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            echo "<input type=hidden name=navegacao value=sim>";
            echo "<input type=hidden name=indice value=$indice>";
        }
        echo "<input type=hidden name=dim value=".($dim).">";
        for ($i=0; $i<$dim; $i++) {
        for ($j=0; $j<$dim; $j++) {
            echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
        } //end for j
        echo "<input type=hidden name=A[".($i)."][".($j)."] value=".($Atemp[$i][$j]).">";
            } //end for i
        echo "<input class=button type=submit name=Calcular value='".$msg->get('GAUSS_MSG_03b')."'>";
            echo "</form>";
    }
}
?>
</div>